var express = require('express');
var expressWs = require("express-ws");
var bodyParser = require('body-parser');
// 创建项目实例
const app = express();
// 加载路由控制
var routes = require('./routes/index');


// 定义数据解析器
// parse application/x-www-form-urlencoded           body-parser  插件 数据问题
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());

// 跨域等
app.all('*splat', function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header('Access-Control-Allow-Headers', ['mytoken', 'Content-Type']);
    next();
});

expressWs(app);  //将 express 实例上绑定 websocket 的一些方法


// 匹配路径和路由
app.use('/', routes);


app.listen('8080', function () {
    console.log("8080");
})
// app.listen(3000); 
module.exports = app;
